package control;

import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import view.MemoryCard;
import view.resources.Images;

public class Util {

	private static int iX;
	private static int iY;
	private static int iWidth;
	private static int iHeight;

	public static void centerFrame(Shell shell) {

		Monitor primary = shell.getDisplay().getPrimaryMonitor();

		iHeight = primary.getClientArea().height;
		iWidth = primary.getClientArea().width;
		iX = (iWidth / 2) - (shell.getBounds().width / 2);
		iY = (iHeight / 2) - (shell.getBounds().height / 2);

		shell.setLocation(iX, iY);
	}

	public static void checkCards() {

		if (Storage.getInstance().getCards().size() == 2) {
			endTurn();
			Storage.getInstance().increaseTurn();
			if (Storage.getInstance().isPlayerTurn()) {
				// Statusbar -> Gegnername
				Storage.getInstance().setPlayerTurn(false);
			} else {
				// Statusbar -> Spielername
				Storage.getInstance().setPlayerTurn(true);
			}
		}
	}

	private static void endTurn() {
		Storage.getInstance().setTurnPause(true);
		if (validateCards()) {
			if (Storage.getInstance().isPlayerTurn()) {
				Storage.getInstance().increasePlayerPair();
			} else {
				Storage.getInstance().increaseEnemyPair();
			}
		} else {
			for (MemoryCard element : Storage.getInstance().getCards()) {

				element.getButton().setImage(Images.DEFAULT.createImage());
				element.getButton().update();
				element.setbActive(false);
				try {
					Thread.sleep(750);
				} catch (InterruptedException e) {
				}

			}
		}

		Storage.getInstance().getCards().clear();
		Storage.getInstance().setTurnPause(false);
		if (isGameDone()) {
			// Ausgabe Sieger
		}
	}

	public static int getRandomNumber(int low, int high) {

		return (int) ((Math.random() * (high - low)) + low);

	}

	private static boolean isGameDone() {
		if ((Storage.getInstance().getPlayerPairCount() + Storage.getInstance()
				.getEnemyPairCount()) == 18) {
			return true;
		}
		return false;
	}

	private static boolean validateCards() {
		MemoryCard one = Storage.getInstance().getCards().get(0);
		MemoryCard two = Storage.getInstance().getCards().get(1);

		if (one.getImageNumber() == two.getImageNumber()) {
			return true;
		}
		return false;

	}

	public static boolean validateInput(String text) {
		if (text.trim().isEmpty() || (text == null)) {
			return false;
		}
		return true;

	}

	private Util() {

	}

}
